package com.dianxin.smartpark.service.property.impl;

import java.util.List;

import com.chinatelecom.response.common.CommonResponseBody;
import com.chinatelecom.response.utils.BeanUtil;
import com.chinatelecom.response.utils.ResponseBodyWrapperUtil;
import com.chinatelecom.response.constant.ErrMsgEnum;

import com.chinatelecom.response.utils.StringUtils;
import com.dianxin.smartpark.dao.dataobject.property.SmartParkPropertyRepair;
import com.dianxin.smartpark.dao.dataobject.property.SmartParkPropertyRepairExample;
import com.dianxin.smartpark.dao.mapper.property.SmartParkPropertyRepairMapper;
import com.dianxin.smartpark.dao.qo.property.SmartParkPropertyRepairQO;
import com.dianxin.smartpark.manager.order.ISmartParkOrderRecordService;
import com.dianxin.smartpark.service.property.SmartParkPropertyRepairService;
import com.dianxin.smartpark.service.utils.OrderNoUtil;
import com.dianxin.smartpark.service.vo.property.SmartParkPropertyRepairVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 报修管理业务层处理
 * 
 * @author chenjiasong
 * @date 2021-07-22
 */
@Service
public class SmartParkPropertyRepairServiceImpl implements SmartParkPropertyRepairService
{
    private final static Logger logger = LoggerFactory.getLogger(SmartParkPropertyRepairServiceImpl.class);
    @Resource
    private SmartParkPropertyRepairMapper smartParkPropertyRepairMapper;

    @Resource
    private ISmartParkOrderRecordService iSmartParkOrderRecordService;

    /**
     * 查询报修管理
     *
     * @param qo 报修管理ID
     * @return 报修管理
     */
    @Override
    public CommonResponseBody<SmartParkPropertyRepairVO> selectSmartParkPropertyRepair(SmartParkPropertyRepairQO qo)
    {
        if(StringUtils.isNull(qo.getId())){
            return ResponseBodyWrapperUtil.fail(ErrMsgEnum.ERROR_PARAME);
        }
        SmartParkPropertyRepairExample smartParkPropertyRepairExample = new SmartParkPropertyRepairExample();
        SmartParkPropertyRepairExample.Criteria criteria = smartParkPropertyRepairExample.createCriteria();
        if (qo.getTenantId() != null) {
            criteria.andTenantIdEqualTo(qo.getTenantId());
        }
        criteria.andIsDeletedEqualTo(0);
        criteria.andIdEqualTo(qo.getId());
        List<SmartParkPropertyRepair> passRecords = smartParkPropertyRepairMapper.selectByExample(smartParkPropertyRepairExample);
        return ResponseBodyWrapperUtil.successWrapper(StringUtils.isNotEmpty(passRecords)?passRecords.get(0):null,SmartParkPropertyRepairVO.class);
    }

    /**
     * 查询报修管理列表
     * 
     * @param qo 报修管理
     * @return 报修管理
     */
    @Override
    public CommonResponseBody<List<SmartParkPropertyRepairVO>> selectSmartParkPropertyRepairList(SmartParkPropertyRepairQO qo)
    {
        SmartParkPropertyRepairExample smartParkPropertyRepairExample = new SmartParkPropertyRepairExample();
        SmartParkPropertyRepairExample.Criteria criteria = smartParkPropertyRepairExample.createCriteria();
        if (qo.getTenantId() != null) {
            criteria.andTenantIdEqualTo(qo.getTenantId());
        }
        criteria.andIsDeletedEqualTo(0);
        if (qo.getNeedPage()) {
            smartParkPropertyRepairExample.setOrderByClause("gmt_create desc limit " + (qo.getPageIndex() - 1) * qo.getPageSize() + "," + qo.getPageSize());
        } else {
            smartParkPropertyRepairExample.setOrderByClause("gmt_create desc");
        }
        List<SmartParkPropertyRepair> passRecords = smartParkPropertyRepairMapper.selectByExample(smartParkPropertyRepairExample);
        long count = smartParkPropertyRepairMapper.countByExample(smartParkPropertyRepairExample);
        return ResponseBodyWrapperUtil
                .successListWrapper(passRecords, count, qo, SmartParkPropertyRepairVO.class);
    }

    /**
     * 新增报修管理
     * 
     * @param vo 报修管理
     * @return 结果
     */
    @Override
    public CommonResponseBody<Boolean> insertSmartParkPropertyRepair(SmartParkPropertyRepairVO vo)
    {
        // 验证 todo
        SmartParkPropertyRepair smartParkPropertyRepair = new SmartParkPropertyRepair();
        try {
            BeanUtil.insertConversion(vo, smartParkPropertyRepair);
        } catch (Exception e) {
            logger.error("", e);
        }
        smartParkPropertyRepair.setStatus(0);
        String orderNo = OrderNoUtil.getOrderNo("WX");
        smartParkPropertyRepair.setOrderNo(orderNo);
        smartParkPropertyRepair.beforeInsert();
        smartParkPropertyRepair.setCreator("1435058739601235968");
        int res = smartParkPropertyRepairMapper.insertSelective(smartParkPropertyRepair);
        if(res > 0){
            res = iSmartParkOrderRecordService.insertSmartParkOrderRecord(vo.getTenantId(),0,vo.getCreator(),smartParkPropertyRepair.getId(),2,1,"提交","提交");
        }
        return ResponseBodyWrapperUtil.toResData(res);
    }

    /**
     * 修改报修管理
     * 
     * @param vo 报修管理
     * @return 结果
     */
    @Override
    public CommonResponseBody<Boolean> updateSmartParkPropertyRepair(SmartParkPropertyRepairVO vo)
    {
        // 验证 todo
        SmartParkPropertyRepair smartParkPropertyRepair = new SmartParkPropertyRepair();
        try {
            BeanUtil.insertConversion(vo, smartParkPropertyRepair);
        } catch (Exception e) {
            logger.error("", e);
        }
        smartParkPropertyRepair.beforeUpdate();
        return ResponseBodyWrapperUtil.toResData(smartParkPropertyRepairMapper.updateByPrimaryKeySelective(smartParkPropertyRepair));
    }

    /**
     * 删除报修管理信息
     *
     * @param vo 报修管理
     * @return 结果08
     */
    @Override
    public CommonResponseBody<Boolean> deleteSmartParkPropertyRepair(SmartParkPropertyRepairVO vo)
    {
        // 验证 todo
        if(StringUtils.isNull(vo.getId())){
            return ResponseBodyWrapperUtil.fail(ErrMsgEnum.ERROR_PARAME);
        }
        SmartParkPropertyRepair smartParkPropertyRepair = new SmartParkPropertyRepair();
        try {
            BeanUtil.insertConversion(vo, smartParkPropertyRepair);
        } catch (Exception e) {
            logger.error("", e);
        }
        smartParkPropertyRepair.beforeDelete();
        return ResponseBodyWrapperUtil.toResData(smartParkPropertyRepairMapper.updateByPrimaryKeySelective(smartParkPropertyRepair));
    }
}
